// 引入pinia数据库创建方法
import { defineStore } from "pinia"
// 引入定义的操作请求
import { getRoleList,addRole, updateRole } from "@/api/role"
// 引入角色(类型)
import { RoleState } from "./types/type"
// 引入自定义数据类型
import type { roleInfoData, getRoleListResponseData,addRoleResponseData } from "@/api/role/type"
// 创建Role角色库，库类型为RoleState,实例化为useRoleStore
let useRoleStore = defineStore("Role", {
    state: (): RoleState => {
        return {
            roles:[]
        }
    },
    // 根据请求结果状态值判断返回错误信息
    actions:{
    // 读取角色列表
    async roleList(){
        let result:getRoleListResponseData = await getRoleList();
        if(result.status === 0){
            this.roles=result.data
            return "ok"
        }else{
            return Promise.reject(new Error(result.msg))
        }
    },
    // 添加
    async useAddRole(data:roleInfoData){
            let result:addRoleResponseData =await addRole(data);
            if(result.status ===0){
            return "OK"
            }else{
            return Promise.reject(new Error(result.msg));
            }
    },
    // 权限变更
    async useUpdateRole(data:roleInfoData){
        let result:addRoleResponseData=await updateRole(data)
        console.log(result);
        
        if(result.status ===0){
            return "ok"
        }
        else{
            return Promise.reject(new Error(result.msg))
        }
    },
},
    getters: {}
})
export default useRoleStore;
